<?php
class UsersController extends AppController {
	
	var $name = 'Users';
//	var $components = array ('Auth', 'Acl' ); // Not necessary if declared in your app controller
	var $layout = 'admin';
	
	/**
	 *  The AuthComponent provides the needed functionality
	 *  for login, so you can leave this function blank.
	 */
	function beforeFilter() {
		$this->Auth->loginAction = array ('controller' => 'users', 'action' => 'login' );
		$this->Auth->loginRedirect = array ('controller' => 'contents', 'action' => 'index' );
		$this->Auth->logoutRedirect = array ('controller' => 'users', 'action' => 'login' );
		$this->Auth->loginError = 'The information you\'ve entered is incorrect.';
		$this->Auth->logoutError = 'You\'ve successfully logged out.';
		$this->Auth->allow ( 'login', 'logout' );
		//echo  $this->Auth->password('a') ;6dd25d865031f6a39cc950ba6fedfbe26a9908b9 
	

	}
	
	function login() {
		
		
		$this->layout = 'login';
		$this->render ( '/elements/empty' );
	}
	
	function logout() {
		$this->layout = 'login';
		$this->Session->setFlash ( 'You\'ve successfully logged out.' );
		$this->render ( '/elements/empty' );
		// $this->redirect($this->Auth->logout());
	}
	
	public function index() {
		$params['order'] = 'User.id';
		if(isset($this->params['named']['orderBy']) ){
			$params['order'] = $this->params['named']['orderBy'];
			$this->set('orderBy', $this->params['named']['orderBy']);
			if(isset($this->params['named']['DESC'])){
				$params['order'] .= ' DESC';
				$this->set('DESC', $this->params['named']['DESC']);
			}
		}
		$users = $this->User->find ( 'all' , $params);
		$this->set ( 'users', $users );
	}
	
	public function delete($id = NULL) {
		if ($id) {
			if ($this->User->delete ( $id )) {
				$this->Session->setFlash ( 'User deleted successfuly!' );
				$this->redirect ( array ('action' => 'index' ) );
			} else {
				$this->Session->setFlash ( 'Failed! Please try again or contact to administrator.' );
				$this->redirect ( array ('action' => 'index' ) );
			}
		
		} else {
			$this->Session->setFlash ( 'Failed! No such user.' );
			$this->redirect ( array ('action' => 'index' ) );
		
		}
	}
	
	public function add() {
		$this->set('userGroups', $this->User->Group->find("list"));
		if (! empty ( $this->data )) {
			$this->User->create ();
			if ($this->User->save ( $this->data )) {
				$this->Session->setFlash ( 'User added successfuly!' );
				$this->redirect ( array ('action' => 'index' ) );
			} else {
				$this->Session->setFlash ( 'Failed to add user!' );
			}
		}
	}
	
	public function edit($id) {
		if (! empty ( $this->data )) {
			$this->User->id = $id;
			if ($this->User->save ( $this->data )) {
				$this->Session->setFlash ( 'User edited successfuly!' );
				$this->redirect ( array ('action' => 'index' ) );
			} else {
				$this->Session->setFlash ( 'Failed to edit user!' );
			}
		} else {
			$this->data = $this->User->findById ( $id );
		}
	}

}

?>